package com.qmx.qmxapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.JoinMeeting;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.bean.MeetingListModel;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.realm.TokenList;
import com.qmx.qmxapp.token.TokenManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe 会议详情
 */
public class LookMeetingDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_meeting_name)
    TextView tv_meeting_name;
    @BindView(R.id.tv_meeting_time)
    TextView tv_meeting_time;
    @BindView(R.id.tv_meeting_host)
    TextView tv_meeting_host;
    @BindView(R.id.tv_meeting_details)
    TextView tv_meeting_details;
    @BindView(R.id.tv_meeting_persons)
    TextView tv_meeting_persons;
    @BindView(R.id.tv_right_label)
    ImageView tv_right_label;
    @BindView(R.id.btn_join_meeting)
    Button btn_join_meeting;
    @BindView(R.id.btn_host_meeting)
    Button btn_host_meeting;
    @BindView(R.id.type_meeting)
    LinearLayout type_meeting;
    @BindView(R.id.btn_to_live)
    Button btn_to_live;

    private Gson mGson = new Gson();
    private String roomId = "";
    private String id = "";
    private Realm realm;
    private String type;//会议类型
    private String hostType;//主持会议类型


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_meeting_details);
        ButterKnife.bind(this);
        realm = Realm.getDefaultInstance();
        initData(this.getIntent().getExtras());

    }

    private void initData(Bundle bundle) {
        StringBuffer stringBuffer = new StringBuffer();
        type = bundle.getString("type");
        if (type.equals("meeting")) {
            type_meeting.setVisibility(View.VISIBLE);
            btn_to_live.setVisibility(View.INVISIBLE);
        } else if (type.equals("live")) {
            type_meeting.setVisibility(View.INVISIBLE);
            btn_to_live.setVisibility(View.VISIBLE);
        }
        MeetingListModel.Wait.Data data = mGson.fromJson(bundle.getString("content"), MeetingListModel.Wait.Data.class);
        //        Log.i("initData==", bundle.getString("content"));
        roomId = String.valueOf(data.getRoom_id());
        if (sharedPreferences.getInt(AppConstant.ROOM_ID, 0) == data.getRoom_id()) {
            hostType = "self";
        } else {
            hostType = "other";
        }

        id = String.valueOf(data.getId());
        tv_meeting_name.setText(data.getTitle());
        tv_meeting_time.setText("时间：" + bundle.getString("time"));
        tv_meeting_host.setText("发起人：" + data.getAdmin().getName());
        tv_meeting_details.setText(data.getDetail());

        for (int i = 0; i < data.getNames().size(); i++) {
            if (i == data.getNames().size() - 1) {
                stringBuffer.append(data.getNames().get(i));
            } else {
                stringBuffer.append(data.getNames().get(i)).append(",");
            }
        }
        tv_meeting_persons.setText(stringBuffer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.isClosed();
    }

    @OnClick({R.id.tv_right_label, R.id.btn_join_meeting, R.id.btn_host_meeting, R.id.btn_to_live})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right_label:
                showToast("功能待开发");
                break;
            case R.id.btn_join_meeting:
                token(realm);
                joinMeeting();
                break;
            case R.id.btn_host_meeting:
                token(realm);
                hostMeeting();
                break;
            case R.id.btn_to_live:
                token(realm);
                liveMeeting();
                break;
        }
    }

    public void joinMeeting() {
        OkGo.<LzyResponse<JoinMeeting>>post(AppConstant.Url + ComParamContact.Reserve.PATH)
                .params(ComParamContact.Reserve.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.Reserve.__TOKEN__, __TOKEN__)
                .params(ComParamContact.Reserve.ID, id)
                .params(ComParamContact.Reserve.CLIENT_ID, sharedPreferences.getString(AppConstant.CLIENT_ID, ""))
                .params(ComParamContact.Reserve.PASSWORD, "")
                .execute(new JsonCallback<LzyResponse<JoinMeeting>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<JoinMeeting>> response) {
                        String json = mGson.toJson(response.body().data);
                        Intent intent = new Intent(LookMeetingDetailsActivity.this, RoomActivity.class);
                        Log.e("LookMeetingDetailsAc", json);
                        intent.putExtra("roomId", roomId);
                        startActivity(intent);
                    }

                    @Override
                    public void onError(Response<LzyResponse<JoinMeeting>> response) {
                        Log.i("onError", response.getException().getMessage());
                    }
                });
    }

    /**
     * 场外主持
     */
    public void hostMeeting() {
        OkGo.<LzyResponse<JoinMeeting>>post(AppConstant.Url + ComParamContact.offMeeting.PATH)
                .params(ComParamContact.offMeeting.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.offMeeting.__TOKEN__, __TOKEN__)
                .params(ComParamContact.offMeeting.ROOM_ID, roomId)
                .params(ComParamContact.offMeeting.TYPE, hostType)
                .params(ComParamContact.offMeeting.CLIENT_ID, sharedPreferences.getString(AppConstant.CLIENT_ID, ""))
                .params(ComParamContact.offMeeting.PASSWORD, "")
                .execute(new JsonCallback<LzyResponse<JoinMeeting>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<JoinMeeting>> response) {
                        String json = mGson.toJson(response.body().data);
                        Intent intent = new Intent(LookMeetingDetailsActivity.this, OffMeetingActivity.class);
                        Log.e("InitiateMeetingActivity", roomId);
                        intent.putExtra("roomId", roomId);
                        startActivity(intent);
                    }

                    @Override
                    public void onError(Response<LzyResponse<JoinMeeting>> response) {
                        Log.i("onError", response.getException().getMessage());
                    }
                });
    }

    /**
     * 直播会议
     */
    public void liveMeeting() {
        OkGo.<LzyResponse<JoinMeeting>>post(AppConstant.Url + ComParamContact.Live.PATH)
                .params(ComParamContact.Live.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.Live.__TOKEN__, __TOKEN__)
                .params(ComParamContact.Live.ID, id)
                .params(ComParamContact.Live.CLIENT_ID, sharedPreferences.getString(AppConstant.CLIENT_ID, ""))
                .params(ComParamContact.Live.PASSWORD, "")
                .execute(new JsonCallback<LzyResponse<JoinMeeting>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<JoinMeeting>> response) {
                        //                        String json = mGson.toJson(response.body().data);
                        if (response.body().msg.equals("断线重连呼入")) {
                            showToast(response.body().msg);
                        } else {
                            Intent intent = new Intent(LookMeetingDetailsActivity.this, RoomActivity.class);
                            Log.e("InitiateMeetingActivity", roomId);
                            intent.putExtra("roomId", roomId);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<JoinMeeting>> response) {
                        Log.i("onError", response.getException().getMessage());
                    }
                });
    }
}
